<template>
  <div id="app">
    <el-col :span="8" style="margin: 0 auto;">
      <el-card shadow="hover" style="width: 624px;height: 733px;position: fixed;top: 50%;left: 50%;margin-left: -412px;margin-top: -311px;border-radius: 20px">
        <div>
          <el-form :model="ruleForm" :rules="rules" label-width="70px" ref="ruleForm" class="demo-ruleFrom" style="width: 400px;margin: 50px auto">
            <h2 style="text-align: center">用户资料修改</h2><br>
            <el-form-item label="用户名" prop="username">
              <el-input type="text" placeholder="请修改用户名" v-model="ruleForm.username"></el-input>
            </el-form-item><br>
            <el-form-item label="密码" prop="password">
              <el-input type="password" placeholder="请修改密码" v-model="ruleForm.password"></el-input>
            </el-form-item><br>
            <el-form-item label="性别" prop="sex">
              <el-input type="text" placeholder="请修改性别" v-model="ruleForm.sex"></el-input>
            </el-form-item><br>
            <el-form-item label="年龄" prop="age">
              <el-input type="text" placeholder="请修改年龄" v-model="ruleForm.age"></el-input>
            </el-form-item><br>
            <el-form-item label="手机" prop="telephone">
              <el-input type="text" placeholder="请修改手机号" v-model="ruleForm.telephone"></el-input>
            </el-form-item><br>
            <el-form-item label="邮箱" prop="email">
              <el-input type="text" placeholder="请修改邮箱" v-model="ruleForm.email"></el-input>
            </el-form-item><br>
            <el-form-item>
              <el-button style="border-radius: 12px; position: relative;right: -100px;" type="primary" @click="submitForm('ruleForm')">保存</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </el-col>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      savedUserId:'',
      ruleForm: {
        username: '',
        password: '',
        sex: '',
        age: 0,
        telephone: '',
        email: '',
        userId:'',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, message: '长度在 2 到 ... 个字符', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请输入性别', trigger: 'blur' },
          { min: 1, max: 1, message: '长度为 1 个字符', trigger: 'blur' }
        ],
        age: [
          {  message: '请输入年龄', trigger: 'blur' },
          {  type: 'number' ,min: 0, max: 100, message: '年龄在0-100岁之间', trigger: 'blur' }
        ],
        telephone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '长度为 11 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 4, max: 30, message: '正确的字符格式', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    loadUserId(){
      // this.userId = this.$route.query.userId;
       this.savedUserId = localStorage.getItem('userId');
    },
    loadUserInfo(){
      let url = 'http://localhost:17701/user/standard?id='+this.savedUserId;
      console.log('url = ' + url);
      let localJwt = JSON.parse(localStorage.getItem('localJwt'));
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt.token
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.ruleForm = jsonResult.data;
          console.log(this.ruleForm)
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:17701/user/update';
          console.log('url = ' + url);

          let formData = new FormData();
          for (let key in this.ruleForm) {
            formData.append(key, this.ruleForm[key]);
          }
          console.log('formData = ', formData.toString());

          axios.post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state === 20000) {
              this.$message({
                message: '保存成功！',
                type: 'success'
              });
              this.$router.push('/');
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          }).catch((error) => {
            this.$router.push('/');
            console.error('保存失败：', error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadUserId();
    this.loadUserInfo();
  }
}
</script>

<style>
body {
  background-image: url('/public/images/index/ancient05.jpg');
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center 0px ;

}
</style>